package com.gilight.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gilight.base.base.ParameterException;
import com.gilight.base.component.LockComponent;
import com.gilight.base.component.LockConst;
import com.gilight.base.dao.ColdOrderDetailMapper;
import com.gilight.base.dao.ColdOrderMapper;
import com.gilight.base.dao.ColdProductMapper;
import com.gilight.base.dto.ColdPayOrderDto;
import com.gilight.base.model.ColdOrder;
import com.gilight.base.model.ColdOrderDetail;
import com.gilight.base.model.ColdProduct;
import com.gilight.base.service.IColdOrderService;
import com.gilight.base.util.GeneratorUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ColdOrderServiceImpl extends ServiceImpl<ColdOrderMapper, ColdOrder> implements IColdOrderService {

    @Autowired
    private ColdProductMapper coldProductMapper;
    @Autowired
    private ColdOrderMapper coldOrderMapper;
    @Autowired
    private ColdOrderDetailMapper coldOrderDetailMapper;

    @Autowired
    private LockComponent lockComponent;


    @Value("${com.iotechn.unimall.machine-no}")
    private String MACHINE_NO;

    @Value("${com.iotechn.unimall.env}")
    private String ENV;
    @Override
    @Transactional
    public Long createOrder(ColdPayOrderDto coldPayOrderDto) {
        //加上乐观锁，防止用户重复提交订单
        if (lockComponent.tryLock(LockConst.TAKE_ORDER_LOCK + coldPayOrderDto.getOpenId(), 20)) {
            Double realPrice = coldPayOrderDto.getRealPrice();
            if(realPrice<=0){
                throw new ParameterException("订单金额必须大于0");
            }
            List<ColdOrderDetail> detailList = coldPayOrderDto.getOrderDetail();
            if(CollectionUtils.isEmpty(detailList)){
                throw new ParameterException("商品详情不能为空");
            }
            Double dbTotalPrice = 0d;
            for (ColdOrderDetail coldOrderDetail:detailList) {
                Double price = coldOrderDetail.getProductPrice();
                ColdProduct coldProduct = coldProductMapper.selectById(coldOrderDetail.getProductId());
                Double dbPrice = coldProduct.getPrice();
                if(dbPrice.compareTo(price)!=0){
                    throw new ParameterException("商品单价不一致");
                }
                if(coldOrderDetail.getProductNumber()<=0){
                    throw new ParameterException("商品数量必须大于0");
                }
                dbTotalPrice += dbPrice*coldOrderDetail.getProductNumber();
            }
            if(dbTotalPrice.compareTo(realPrice)!=0){
                throw new ParameterException("订单价格有误");
            }
            int childIndex = 1;
            String parentOrderNo = GeneratorUtil.genOrderId(this.MACHINE_NO, this.ENV);
            String childOrderNo = parentOrderNo + "S" + ((1000) + childIndex);
            //创建订单
            ColdOrder coldOrder = new ColdOrder();
            coldOrder.setParentOrderNo(parentOrderNo);
            coldOrder.setOrderNo(childOrderNo);
            coldOrder.setOrderPrice(coldPayOrderDto.getOrderPrice());
            coldOrder.setOrderTime(new Date());
            coldOrder.setDiscountPrice(coldPayOrderDto.getDiscountPrice());
            coldOrder.setRealPrice(coldPayOrderDto.getRealPrice());
            coldOrder.setZzjgId(coldPayOrderDto.getZzjgId());
            coldOrder.setOpenId(coldPayOrderDto.getOpenId());
            coldOrderMapper.insertSelective(coldOrder);
            //添加订单详情
            for (ColdOrderDetail coldOrderDetail:detailList) {
                coldOrderDetail.setOrderId(coldOrder.getId());
                coldOrderDetailMapper.insertSelective(coldOrderDetail);
            }
            return coldOrder.getId();
        }
        return null;
    }

    @Override
    public List<ColdPayOrderDto> getOrderList(ColdOrder coldOrder) {
        return coldOrderMapper.getOrderList(coldOrder);
    }

    @Override
    public List<ColdOrderDetail> getOrderDetail(Long orderId) {
        return coldOrderDetailMapper.getOrderDetail(orderId);
    }

    @Override
    public ColdOrder getOrder(Long orderId) {
        return coldOrderMapper.selectByPrimaryKey(orderId);
    }

}
